package com.acertificate.domain;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TableRow;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.acertificate.entities.AbsWrap;

public class Com60ph {

	public static final String URL_PREFIX = "http://www.60ph.com/List_feizhuliu/3387407_77_";
	public static final String SUFFIX = ".html";

	public static final String ASDF = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\">";

	public static final String SQL_TEMPLATE = "INSERT INTO db_content_article VALUES (NULL, '%s', '0', '%s', '', '', '', '', '', '1', '2', '0', '1331793273', '0', '1', '1', '19', '0');";

	public static int maxProcess = 20;
	public static int total = 97;
	public static int threadCount;

	public static String[] tc = { "<TR>          <TD><STYLE>.Rah131 {	DISPLAY: none}</STYLE>", "</TD></TR>" };

	{
		threadCount = total / maxProcess + (total % maxProcess == 0 ? 0 : 1);
	}

	public static void main(String[] args) throws ParserException, MalformedURLException, Exception {
		Com60ph wanmv = new Com60ph();
		for (int i = 1; i <= threadCount; i++) {
			Com60phThread thread = wanmv.new Com60phThread((i - 1) * maxProcess + 1, (i * maxProcess > total ? total : i * maxProcess));
			thread.start();
		}
	}

	class Com60phThread extends Thread {

		public int start;
		public int end;

		public Com60phThread() {
		}

		public Com60phThread(int start, int end) {
			this.start = start;
			this.end = end;
		}

		public void run() {
			for (int i = start; i < end; i++) {
				getManifest(i, URL_PREFIX + i + SUFFIX);
			}
		}

		public List<AbsWrap> getManifest(int index, String url) {
			List<AbsWrap> results = new ArrayList<AbsWrap>();
			try {
				URLConnection urlConnection = new URL(url).openConnection();
				String content = IOUtils.toString(urlConnection.getInputStream(), "gb2312");
				Parser parser = new Parser();
				parser.setInputHTML(content.replace(ASDF, ""));
				AndFilter andFilter = new AndFilter(new NodeClassFilter(Div.class), new HasAttributeFilter("class", "list"));
				NodeList list = parser.extractAllNodesThatMatch(andFilter);
				for (int i = 0; i < list.size(); i++) {
					Node tag = list.elementAt(i);
					parser.setInputHTML(tag.toHtml());
					NodeList links = parser.extractAllNodesThatMatch(new NodeClassFilter(LinkTag.class));
					for (int x = 0; x < links.size(); x++) {
						Node taglink = links.elementAt(x);
						if (taglink instanceof LinkTag) {
							LinkTag link = (LinkTag) taglink;
							AbsWrap wrap = new AbsWrap();
							wrap.setTitle(link.getLinkText());
							wrap.setUrl(link.getLink());
							wrap.setContent(getContent(link.getLink()));
							results.add(wrap);
						}
					}
				}
				StringBuffer sb = new StringBuffer();
				for (AbsWrap wrap : results) {
					sb.append(String.format(SQL_TEMPLATE, new Object[] { wrap.getTitle(), wrap.getContent() }));
					sb.append("\r\n\r\n");
				}
				try {
					String fileName = "hsjq-p-" + System.currentTimeMillis() + ".txt";
					System.out.println("generating SQL files " + fileName + " ...");
					IOUtils.write(sb.toString(), new FileOutputStream(new File("c:\\domains\\60ph/" + fileName)));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return results;
		}

		public String getContent(String url) throws Exception {
			url = url.replace("..", "http://www.60ph.com");
			System.out.println(url);
			String content = null;
			Parser contentParser = new Parser();
			URLConnection urlConnection = new URL(url).openConnection();
			String content1 = IOUtils.toString(urlConnection.getInputStream(), "gb2312");
			contentParser.setInputHTML(content1.replace(ASDF, ""));
			NodeList nodes = contentParser.parse(new HasAttributeFilter("class", "c5"));
			if (nodes.size() > 1) {
				for (int i = 0; i < nodes.size(); i++) {
					Node node = nodes.elementAt(i);
					content = node.toHtml();
				}
			} else {
				contentParser.reset();
				NodeList xx = contentParser.parse(new NodeClassFilter(TableRow.class));
				for (int i = 0; i < 1; i++) {
					Node node = xx.elementAt(i);
					content = node.toHtml();
					content = content.replaceAll("\r\n", "");
					for (String str : tc) {
						content = content.replace(str, "");
					}
				}
			}
			return content;
		}
	}

}
